package com.zjzaki.thingsboard.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zjzaki.thingsboard.R;
import com.zjzaki.thingsboard.entity.Device;
import com.zjzaki.thingsboard.entity.DeviceServerScope;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ListViewAdapter extends BaseAdapter {

    private List<Device> data;
    private LayoutInflater inflater;
    private List<DeviceServerScope> deviceServerScopeList;

    /**
     * 构造函数
     *
     * @param context 应用上下文
     * @param data    数据列表
     */
    public ListViewAdapter(Context context, List<Device> data) {
        inflater = LayoutInflater.from(context);
        this.data = data;
    }

    /**
     * 构造函数
     *
     * @param context               应用上下文
     * @param data                  数据列表
     * @param deviceServerScopeList 设备服务端属性列表
     */
    public ListViewAdapter(Context context, List<Device> data, List<DeviceServerScope> deviceServerScopeList) {
        inflater = LayoutInflater.from(context);
        this.data = data;
        this.deviceServerScopeList = deviceServerScopeList;
    }


    /**
     * 得到数据源长度
     *
     * @return 数据源长度
     */
    @Override
    public int getCount() {
        return data.size();
    }

    /**
     * 每一行绑定数据源
     *
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获取每一行的view
     *
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            //xml加载成View
            convertView = inflater.inflate(R.layout.device_view, parent, false);
            //加载里面的元素
            //viewHolder.img = convertView.findViewById(R.id.img_01);
            viewHolder.txt_name = convertView.findViewById(R.id.txt_name);
            viewHolder.txt_create_time = convertView.findViewById(R.id.txt_create_time);
            viewHolder.device_state = convertView.findViewById(R.id.device_state);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //设置设备名
        viewHolder.txt_name.setText(data.get(position).getName());
        //时间转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date(data.get(position).getCreatedTime()));
        //设置时间
        viewHolder.txt_create_time.setText("创建时间：" + time);
        //设置设备的在线状态
        if (deviceServerScopeList != null && deviceServerScopeList.size() != 0) {
            System.out.println("进入设置设备状态");
            //得到最后断开连接的时间戳
            Long lastDisconnectTime = deviceServerScopeList.get(position).getLastDisconnectTime();
            //得到最后连接的时间戳
            Long lastConnectTime = deviceServerScopeList.get(position).getLastConnectTime();
            //判断两个值是否为null
            if (lastConnectTime != null && lastDisconnectTime != null) {
                //如果最后断开的时间大于最后连接的时间，则设备离线
                if (lastDisconnectTime > lastConnectTime) {
                    viewHolder.device_state.setTextColor(Color.RED);
                    viewHolder.device_state.setText("离线");
                } else {
                    //如果最后连接时间大于最后断开时间，则代表设备上线
                    viewHolder.device_state.setTextColor(Color.BLACK);
                    viewHolder.device_state.setText("在线");
                }
            }
        }
        return convertView;
    }

    /**
     * 定义类
     */
    private class ViewHolder {
        private TextView txt_name;
        private TextView txt_create_time;
        private ImageView img;
        private TextView device_state;
    }
}
